<template>
  <!-- <div class="square_header2 flex align_center">
    <div class="square_header_item square_header_item2" :class="form.type == index ? 'underline ' : ''"
      v-for="(item, index) in type" :key="index" @click="checkActiveType(index)">
      <div :class="form.type == index ? 'content4 color1 zIndex fontBold ' : 'default2'">{{ item.name }}</div>
    </div>
  </div> -->
  <van-tabs @click-tab="clickTab" v-model:active="form.type" title-active-color="#12142e"
    title-inactive-color="#454757" background="#f7f7f7" color="#ff7a1f" >
    <van-tab :title="item.name" v-for="(item, index) in type" :key="index"></van-tab>
  </van-tabs>
  <div>
    <van-pull-refresh v-model="refreshing" disabled @refresh="onRefresh" class="refresh-content">
      <van-list class="content-list" v-model:loading="loading" :finished="finished" @load="loadMore"
        :immediate-check="false">
        <div class="college_list" v-for="(item, index) in data" :key="index" @click="goActivity(item.id)">
          <div class="college_list_box">
            <div>
              <img class="college_img" :src="item.tpic">
            </div>
            <div class="college_info">
              <p class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
                v-if="item.active_suatus == 1">
                <van-tag style="height:18px;width:55px;justify-content: center;" class="tagleft" color="#FF7A1F"
                  type="primary">
                  报名中
                </van-tag>
                {{ item.title }}
              </p>
              <p class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
                v-if="item.active_suatus == 2 || item.active_suatus == 3">
                <van-tag style="height:18px;width:55px;justify-content: center;" class="tagleft" color="#85898c"
                  type="primary">
                  {{ item.active_suatus == 2 ? '已截止' : '已结束' }}
                </van-tag>
                {{ item.title }}
              </p>
              <div class="content2 font400 color6">
                <span>报名截止:{{ item.deadline }}</span>
                <!-- <span class=" left12">限时免费</span> -->
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
  <no-data v-if="data.length === 0" />
</template>

<script setup lang="ts">
import { ref, computed, watch, defineAsyncComponent } from 'vue'
import { activityList, getActivityCategory } from '@/api/index'
import { useRouter, useRoute } from 'vue-router'
const NoData = defineAsyncComponent(() => import('@/components/NoData/index.vue'))
const router = useRouter()
const route = useRoute()
const loading: any = ref(false)
const finished: any = ref(false)
const refreshing: any = ref(false)
const data: any = ref([])
const form: any = ref({
  page: 1,
  pageSize: 10,
  type: route.query.type || 0
})
const type: any = ref([
  {
    name: '全部'
  },
  {
    name: '营销活动'
  },
  {
    name: '协会活动'
  },
  {
    name: '行业活动'
  }
])
// const active = ref(0)

getActivityCategory().then((res: any) => {
  type.value = [
    {
      name: '全部'
    },
    ...res
  ]
})

const selectedDemandTypeId = computed(() => {
  return route.query.type || 0
})

watch(selectedDemandTypeId, (newVal, oldVal) => {
  onRefresh()
})

const checkActiveType = (index: any) => {
  form.value.type = index
  router.replace({
    path: '/find',
    query: { f: route.query.f, type: index }
  })
}

const onRefresh = async () => {
  form.value.page = 1
  const list: any = await loadDemandList()
  data.value = list
  refreshing.value = false
}

const loadMore = async () => {
  form.value.page = form.value.page + 1
  const list: any = await loadDemandList()
  data.value = [...data.value, ...list]
  refreshing.value = false
}

const clickTab = (name: any) => {
  checkActiveType(name.name)
}

const loadDemandList = async () => {
  // 异步更新数据
  const res: any = (await activityList({ ...form.value })) || []
  loading.value = false
  if (!Array.isArray(res) && typeof res.list === 'object') {
    const keys: any = Object.keys(res.list)
    const newRes: any = keys.map((k: any) => res.list[k])
    finished.value = newRes.length < form.value.pageSize
    return newRes
  }
  finished.value = res.list.length < form.value.pageSize
  return res.list
}

onRefresh()

const goActivity = async (id: any) => {
  router.push({ path: '/find/activity', query: { id: id } })
}
</script>

<style lang="less" scoped>
::v-deep .van-tabs__nav {
  padding-left: 0;
  padding-right: 0;
}

::v-deep .van-tab--active {
  font-weight: bold;
  font-size: var(--19) !important;
  z-index: 999;
}

::v-deep .van-tab {
  font-size: 14px;
  font-weight: bold;
  color: #454757;
}

::v-deep .van-tabs__line {
  height: 8px;
  z-index: 0;
  bottom: 24px;
}

.home {
  margin: 0 15px;

  .request-btn {
    font-size: 16px;
    border: none;
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2;
    width: 149px;
    height: 44px;
    background: #ff7a1f;
    box-shadow: 0px 2px 10px 0px rgba(238, 111, 42, 0.5);
    border-radius: 5px;
  }

  .onSearch {
    border-radius: 5px;
    margin-bottom: 20px;

    .label_icon {
      overflow: hidden;
      display: flex;
      align-items: center;

      .icon {
        width: 34px;
        height: 34px;
        object-fit: cover;
        margin-right: 14px;
      }

      span {
        margin: 0 14px 0 0;
        color: #d8d8d8;
      }
    }

    .van-field__left-icon {
      display: none;
    }
  }
}

.square_header {
  margin-top: 26px;

  .square_header_item {
    margin-right: 29px;

    .default {
      font-size: 19px;
      font-weight: bold;
      color: #454757;
    }
  }
}

.square_header2 {

  // margin-top: 22px;
  overflow-y: hidden;
  overflow-x: scroll;

  .square_header_item2 {
    margin-right: 32px;

    .default2 {
      font-size: 14px;
      font-weight: bold;
      color: #454757;
    }
  }

  .square_header_item2:last-child {
    margin-right: 0;
  }
}

.college_list {
  width: 100%;
  margin: 15px auto;
  background-color: #fff;
  border-radius: 5px;

  .college_list_box {
    display: flex;
    padding: 15px;

    .college_info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .radio {
        width: 10px;
        height: 10px;
        margin-right: 5px;
      }
    }

    .college_list_box_p {
      line-height: 20px;
    }

    // justify-content: space-between;
    .college_img {
      width: 100px;
      height: 70px;
      border-radius: 5px;
      object-fit: cover;
      margin-right: 14px;
    }

    .left12 {
      margin-left: 10px;
      color: #ee762a;
    }
  }
}

.user-card-item {
  width: 90%;
  height: 87px;
  background: #ffffff;
  border-radius: 10px;
  margin: 10px auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;

  .excellent {
    width: 25px;
    height: 25px;
    margin-right: 14px;
  }

  .left {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-left: 14px;

    .info {
      margin-left: 10px;
    }
  }

  .avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
  }

  .qiye {
    width: 14px;
    height: 14px;
    margin: 0 4px 0 2px;
    display: inline-block;
    vertical-align: middle;
  }

  .position {
    font-size: 12px;
    font-weight: 400;
    color: #9a9a9a;
  }

  .name {
    font-size: 14px;
    font-weight: bold;
    color: #343434;
  }

  .w {
    width: 200px;
    display: inline-block;
    vertical-align: middle;
  }

  .company {
    font-size: 12px;
    font-weight: 400;
    color: #9a9a9a;
    margin: 5px 0;
  }

  .tags {
    margin-right: 9px;
  }

  p {
    margin: 0;
  }
}

@media (min-width: 750px) {
  .home {
    margin: 0 15px;

    .request-btn {
      font-size: 16px;
      border: none;
      position: fixed;
      bottom: 100px;
      left: 50%;
      transform: translateX(-50%);
      z-index: 2;
      width: 149px;
      height: 44px;
      background: #ff7a1f;
      box-shadow: 0px 2px 10px 0px rgba(238, 111, 42, 0.5);
      border-radius: 5px;
    }

    .onSearch {
      border-radius: 5px;
      margin-bottom: 20px;

      .label_icon {
        overflow: hidden;
        display: flex;
        align-items: center;

        .icon {
          width: 34px;
          height: 34px;
          object-fit: cover;
          margin-right: 14px;
        }

        span {
          margin: 0 14px 0 0;
          color: #d8d8d8;
        }
      }

      .van-field__left-icon {
        display: none;
      }
    }
  }

  .square_header {
    margin-top: 26px;

    .square_header_item {
      margin-right: 29px;

      .default {
        font-size: 19px;
        font-weight: bold;
        color: #454757;
      }
    }
  }

  .square_header2 {

    // margin-top: 22px;
    .square_header_item2 {
      margin-right: 32px;

      .default2 {
        font-size: 14px;
        font-weight: bold;
        color: #454757;
      }
    }

    .square_header_item2:last-child {
      margin-right: 0;
    }
  }

  .college_list {
    width: 100%;
    margin: 15px auto;
    background-color: #fff;
    border-radius: 5px;

    .college_list_box {
      display: flex;
      padding: 15px;

      .college_info {
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .radio {
          width: 10px;
          height: 10px;
          margin-right: 5px;
        }
      }

      .college_list_box_p {
        line-height: 20px;
      }

      // justify-content: space-between;
      .college_img {
        width: 100px;
        height: 70px;
        border-radius: 5px;
        object-fit: cover;
        margin-right: 14px;
      }

      .left12 {
        margin-left: 10px;
        color: #ee762a;
      }
    }
  }

  .user-card-item {
    width: 90%;
    height: 87px;
    background: #ffffff;
    border-radius: 10px;
    margin: 10px auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;

    .excellent {
      width: 25px;
      height: 25px;
      margin-right: 14px;
    }

    .left {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-left: 14px;

      .info {
        margin-left: 10px;
      }
    }

    .avatar {
      width: 42px;
      height: 42px;
      border-radius: 50%;
    }

    .qiye {
      width: 14px;
      height: 14px;
      margin: 0 4px 0 2px;
      display: inline-block;
      vertical-align: middle;
    }

    .position {
      font-size: 12px;
      font-weight: 400;
      color: #9a9a9a;
    }

    .name {
      font-size: 14px;
      font-weight: bold;
      color: #343434;
    }

    .w {
      width: 200px;
      display: inline-block;
      vertical-align: middle;
    }

    .company {
      font-size: 12px;
      font-weight: 400;
      color: #9a9a9a;
      margin: 5px 0;
    }

    .tags {
      margin-right: 9px;
    }

    p {
      margin: 0;
    }
  }
}
</style>
